<?php

// +----------------------------------------------------------------------
// | 用户
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 微尘 <yicmf@qq.com>
// +----------------------------------------------------------------------

namespace app\controller\api\v3;


use app\model\ucenter\User as UserModel;
use app\model\pay\Pay as PayModel;
use app\model\ucenter\UserCash as UserCashModel;
use app\model\ucenter\UserMoneyLog as UserMoneyLogModel;

class Team extends Common
{
    public function index()
    {

        $bids = UserModel::where('bid', $this->user['id'])->column('id');
        $info['info']['b_user'] = UserModel::where('bid', $this->user['id'])->count();
        $info['info']['o_user'] = UserModel::where('bid', 'in', $bids)->count();
        $info['info']['all'] = $info['info']['b_user'] + $info['info']['o_user'];
//        $info['b_list'] =  UserModel::where('bid', $this->user['id'])->paginate(50);
        if ('b' == $this->request->param('type', 'b')) {
            $info['b_team'] = UserModel::where('pid|bid', $this->user['id'])
                ->field('id,avatar,level,pid,bid,mobile,nickname,is_effective,is_b_effective,create_time,realName as real_name')->order('id DESC')->paginate(10);
        } else {
            $info['o_team'] = UserModel::where('bid', 'in', $bids)->field('id,avatar,level,pid,bid,mobile,nickname,is_effective,is_b_effective,create_time,realName as real_name')->order('id DESC')->paginate(10);
        }

        return $this->successAjax($info);
    }

    public function join()
    {
        $info['info']['img'] = '/static/recommend/agent.jpg';

//        $info['info']['img'] = $this->request->domain(). '/upload/agent.jpg';
        $info['info']['wechat'] = 'aaaaaa';
        $info['info']['mobile'] = 'aaaaaa';
        $info['info']['wechat_img'] = '/static/recommend/plan2.png';
        $info['server_link'] = 'https://work.weixin.qq.com/kfid/kfc90aba11cd88ea3ea';
        return $this->successAjax($info);
    }

    public function agent()
    {

        $user_id = 0;
        $user_ids = [];
        if (0 == $this->user['is_agency'] && $this->user['is_recommend'] == 1) {
            $user_id = $this->user['id'];
            $agent_id = $this->user['agent_id'];
            $n = UserModel::where('agent_id', $agent_id)->where('pid', $user_id)->column('id');
            if ($n) {
                $user_ids = array_merge($n, [$user_id]);
            } else {
                $user_ids[] = $user_id;
            }
        } else {
            $agent_id = $this->user['id'];
        }

//        $bids = UserModel::where('bid', $this->user['id'])->column('id');
        $data['info']['month']['deposit_amount'] = PayModel::where('agent_id', $agent_id)->where('type', 'deposit')
            ->whereTime('pay_time', 'month')->sum('total_amount');

        $cash = UserCashModel::where('agent_id', $agent_id)
            ->where('user_id', '<>', $agent_id)
            ->where('cash_charge', '>', 2)
            ->whereTime('create_time', 'month')
            ->where('status', 1)->count();
        $data['info']['month']['deposit_amount'] = $data['info']['month']['deposit_amount'] + $cash * 2;
        $data['info']['month']['profit_amount'] = UserMoneyLogModel::where('agent_id', $agent_id)
            ->where('user_id', '<>', $agent_id)
            ->whereTime('create_time', 'month')
            ->where('is_work', '<>', 0)
            ->sum('money');
        $data['info']['month']['user'] = UserModel::where('agent_id', $agent_id)->whereTime('effective_time', 'month')->count();
        if ($data['info']['month']['user'] <= 30) {
            $data['info']['month']['rebate_level'] = 1;
        } elseif ($data['info']['month']['user'] <= 60) {
            $data['info']['month']['rebate_level'] = 2;
        } else {
            $data['info']['month']['rebate_level'] = 3;
        }
        $data['info']['last_month'] = $this->user->bills()->whereTime('bill_time', 'month')->find();;
        // 保障金任务
        $data['job']['register_user'] = UserModel::where('agent_id', $agent_id)->whereTime('create_time', 'year')->count();
        $data['job']['active_user'] = UserModel::where('agent_id', $agent_id)->where('is_effective', 1)->whereTime('create_time', 'year')->count();
        $data['job']['need_user'] = 1000;
        $data['job']['percentage'] = round($data['job']['active_user'] / $data['job']['need_user'], 3) * 100;

        $data['lists'] = UserModel::where('agent_id', $agent_id)->when($user_id, function ($query) use ($user_id, $user_ids) {
            $query->where('pid', 'in', $user_ids);
        })->field('id,nickname,mobile,level,avatar,wechat,create_time')->order('id DESC')->paginate(20);
//        $info['lists'] = UserModel::where('agent_id', null)->field('id,nickname,mobile,level,avatar,wechat')->order('id DESC')->paginate(20);
        return $this->successAjax($data);
    }


}
